package com.yangming.common.base;

import com.yangming.common.utils.SpringUtil;
import com.yangming.model.User;
import com.yangming.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author PengShuiMing
 * @create 2018-09-07
 **/
public class BaseController {
    private static final Logger logger =   LoggerFactory.getLogger(BaseController.class);

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    /**
     * 获取微信标识
     * @return
     */
    protected String getOpenId() {
        String openId = (String) request.getSession().getAttribute(Constants.USER_OPEN_ID_KEY);
        return openId;
    }
    /**
     * 获取微信标识
     * @return
     */
    protected User getUser() {
        User user = null;
        String openId = getOpenId();
        if(StringUtils.isNotBlank(openId)){
            IUserService userService = (IUserService) SpringUtil.getBeanByClass(IUserService.class);
            user = userService.selectUserByOpenId(openId);
        }
        return  user;
    }


}
